maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=regal
pkgver=0.28.0
pkgrel=0
pkgdesc="Linter for Rego files"
url="https://docs.styra.com/regal"
arch="all"
license="Apache-2.0"
makedepends="go"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/StyraInc/regal/archive/refs/tags/v$pkgver.tar.gz"
options="net" # download Go modules

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	# The flag for open-policy-agent is to prevent telemetry report
	local _goldflags="
	-X github.com/styrainc/regal/pkg/version.Version=v$pkgver
	-X github.com/styrainc/regal/pkg/version.Commit=AlpineLinux
	-X github.com/styrainc/regal/pkg/version.Timestamp=$(date -u "+%Y-%m-%dT%H:%M:%S%z" ${SOURCE_DATE_EPOCH:+-d @$SOURCE_DATE_EPOCH})
	-X github.com/styrainc/regal/pkg/version.Hostname=alpinelinux.org
	-X github.com/open-policy-agent/opa/internal/report.ExternalServiceURL=
	"

	go build -v -ldflags "$_goldflags" .

	for shell in bash fish zsh; do
		./regal completion $shell > regal.$shell
	done
}

check() {
	# lsp tests easily times out listening to diagnostic events in slow builds (.e.g riscv64)
	# shellcheck disable=2046
	go test $(go list ./... | grep -v '/internal/lsp$')
}

package() {
	install -Dm755 regal -t "$pkgdir"/usr/bin/

	install -Dm644 regal.bash \
		"$pkgdir"/usr/share/bash-completion/completions/regal
	install -Dm644 regal.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/regal.fish
	install -Dm644 regal.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_regal
}

sha512sums="
fbdbd9d96c9e8388ae68a6ba3c43fbafd0c4be464136a8b06c22b1776df015647c0d19b6d12bddb825a69a767d2c55ec44582e7197c3395b8fb045dd294afc77  regal-0.28.0.tar.gz
"
